using System;
using System.Collections;
using System.Collections.Generic;

namespace Narwhunderful.Aphelia.Framework.IO
{
    internal static class Helpers
    {

        public static bool IsSimple(Type type)
        {
            if (type == typeof(string))
            {
                return true;
            }
            if (type == typeof(DateTime))
            {
                return true;
            }
            if (type == typeof(TimeSpan))
            {
                return true;
            }
            if (type == typeof(Decimal))
            {
                return true;
            }
            if (type == typeof(Guid))
            {
                return true;
            }
            if (type.IsEnum)
            {
                return true;
            }

            return type.IsPrimitive;
        }

        public static bool IsEnumerable(Type type)
        {
            Type referenceType = typeof(IEnumerable);
            return referenceType.IsAssignableFrom(type);
        }

        public static bool IsCollection(Type type)
        {
            Type referenceType = typeof(ICollection);
            return referenceType.IsAssignableFrom(type);
        }

        public static bool IsDictionary(Type type)
        {
            Type referenceType = typeof(IDictionary);
            return referenceType.IsAssignableFrom(type);
        }

        public static bool IsArray(Type type)
        {
            return type.IsArray;
        }

        public static object CreateInstance(Type type)
        {
            if (type == null) return null;

            try
            {
                object result = Activator.CreateInstance(type);
                return result;
            }
            catch (Exception ex)
            {
                throw new CreatingInstanceException(
                    string.Format(
                        "Error during creating an object. Please check if the type \"{0}\" has public parameterless constructor. Details are in the inner exception.",
                        type.AssemblyQualifiedName), ex);
            }
        }
    }
}
